<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/4/18
 * Time: 20:11
 */
namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Page;
use think\Db;

/**
 * 文章控制器
 * Class Article
 * @package app\index\controller
 */
class Article extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index(){
        $cpage=!empty($this->request->param('cpage'))?intval($this->request->param('cpage')):1;
        $total=!empty($this->request->param('total'))?intval($this->request->param('total')):10;
        $cid=!empty($this->request->param('cid'))?intval($this->request->param('cid')):0;
        $tagsids=!empty($this->request->param('tagsids'))?trim($this->request->param('tagsids')):0;
        $keywords=!empty($this->request->param('keywords'))?trim($this->request->param('keywords')):0;

        $where="1=1";

        //分类下所有文章
        $start=($cpage-1)*$total;
        $url='/list/'.$keywords.'/'.$cid.'/'.$tagsids;

        if(!empty($keywords)){
            $where.=" and title like '%".$keywords."%'";
        }

        if(!empty($tagsids)){
            $where.=" and( tags = '".$tagsids."' or tags like '%".$tagsids.','."%' or tags like '%".','.$tagsids."%')";
            $tagsname=db('tags')->where('id='.$tagsids)->value('name');
            $this->assign('tagsname',$tagsname);
        }

        if(!empty($cid)){
            $category_name=db('article_category')->where('id='.$cid)->value('name');
            $where.=" and category_id=".$cid;
            $this->assign('category_name',$category_name);
        }

        $articleList=db('article')->where($where)->order('sort desc,id desc')->field('id,title,thumb,info,from_unixtime(createtime,"%Y-%m-%d") as createtime,avatar,isding,category_id')->limit($start,$total)->select();
        if(!empty($articleList)){
            foreach ($articleList as &$v){
                if(!empty($v['category_id'])){
                    $v['category_name']=db('article_category')->where('id='.$v['category_id'])->value('name');
                }
            }
            unset($v);
        }
        $count=db('article')->where($where)->count();

        //分页
        $page=new Page();
        $show=$page->create($count,$cpage,$total,$url);

        $this->assign('articleList',$articleList);
        $this->assign('show',$show);

        //站长推荐
        $recommendArticleList=db('article')->where('isrecommend=1')->order('sort desc,id desc')->field('id,title')->limit(8)->select();

        //点击排行
        $clicksSortArticleList=db('article')->order('clicks desc,sort desc,id desc')->field('id,title')->limit(8)->select();

        //标签云 随机20个
        $tagsList=Db::query("select id,name from fa_tags order by rand() limit 50");

        $this->assign('recommendArticleList',$recommendArticleList);
        $this->assign('clicksSortArticleList',$clicksSortArticleList);
        $this->assign('keywords',$keywords);
        $this->assign('tagsList',$tagsList);

        return $this->view->fetch();
    }

    public function detail()
    {
        // header("location:http://feicui.zexploit.com/");exit;
        $id=!empty($this->request->param('id'))?intval($this->request->param('id')):0;
        if(empty($id)) $this->error('文章不存在！',url('/index'));

        //文章
        $article=db('article')->where('id='.$id)->field('*,from_unixtime(createtime,"%Y-%m-%d") as createtime')->find();
        if(empty($article)){
            $this->error('文章不存在！',url('/index'));
        }
        //文章评论数量
        $article['commentCount']=db('article_comment')->where('article_id='.$id)->count();

        //文章分类
        $category=array();
        if(!empty($article['category_id'])){
            $category=db('article_category')->where('id='.$article['category_id'])->field('name')->find();
        }

        //文章标签
        if(!empty($article['tags'])){
            $tags=db('tags')->where('id in ('.$article['tags'].')')->column('name');
            $article['tagsStr']=implode(',',$tags);
        }

        //上一篇文章
        $lastArticle=db('article')->where('id<'.$id)->field('id,title')->limit(1)->find();

        //下一篇文章
        $nextArticle=db('article')->where('id>'.$id)->field('id,title')->limit(1)->find();

        //相关文章 即同分类文章
        $relevantArticleList=db('article')->where('id<>'.$id.' and category_id='.$article['category_id'])->order('sort desc,id desc')->field('id,title')->limit(6)->select();

        //点击排行
        $clicksSortArticleList=db('article')->order('clicks desc,sort desc,id desc')->field('id,title')->limit(8)->select();

        //标签云 随机20个
        $tagsList=Db::query("select id,name from fa_tags order by rand() limit 50");

        //猜你喜欢 随机几篇文章
        $guessArticleList=Db::query("select id,title from fa_article order by rand() limit 6");

        //文章评论
        $articleCommentList=db('article_comment')->alias('a')->join('user b','a.user_id = b.id')->where('a.article_id='.$id)->order('a.id desc')->field('a.id,a.content,a.user_id,from_unixtime(a.createtime,"%Y-%m-%d %H:%i:%s") as createtime,b.nickname,b.avatar')->select();

        //查询是否有查看记录
        $ip=$this->request->ip();
        $viewRecordCount=db('view_record')->alias('a')->join('user b','a.user_id=b.id')->where('b.joinip="'.$ip.'" and a.article_id='.$id)->count();

        $this->assign('article',$article);
        $this->assign('category',$category);
        $this->assign('lastArticle',$lastArticle);
        $this->assign('nextArticle',$nextArticle);
        $this->assign('relevantArticleList',$relevantArticleList);
        $this->assign('clicksSortArticleList',$clicksSortArticleList);
        $this->assign('tagsList',$tagsList);
        $this->assign('guessArticleList',$guessArticleList);
        $this->assign('articleCommentList',$articleCommentList);
        $this->assign('viewRecordCount',$viewRecordCount);
        return $this->view->fetch();
    }

}